package sg.edu.nus.iss.billing.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Daniel
 *
 */

public class DateUtil {
	
	public static Date getNonTerminatedEndDate() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.YEAR, 200);
		return cal.getTime();
	}
	
	public static Date getDate(int day, int month, int year) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		return cal.getTime();
	}
	
	public static Date addDateByDay(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, day);
		return cal.getTime();
	}
	
	public static boolean isDateWithin(Date aDate, Date startDate, Date endDate) {
		return aDate.after(startDate) && aDate.before(endDate);
	}
	
	public static long diffInDays(Date aDate, Date anotherDate) {
		Calendar aCal = Calendar.getInstance();
		Calendar anotherCal = Calendar.getInstance();
		aCal.set(Calendar.HOUR_OF_DAY, 0);
		aCal.set(Calendar.MILLISECOND, 0);
		aCal.set(Calendar.MINUTE, 0);
		aCal.set(Calendar.SECOND, 0);

		anotherCal.set(Calendar.HOUR_OF_DAY, 0);
		anotherCal.set(Calendar.MILLISECOND, 0);
		anotherCal.set(Calendar.MINUTE, 0);
		anotherCal.set(Calendar.SECOND, 0);

		aCal.setTime(aDate);
		anotherCal.setTime(anotherDate);
		
		long milis1 = aCal.getTimeInMillis();
		long milis2 = anotherCal.getTimeInMillis();
		
		long diff = milis2 - milis1;
		long diffDays = diff / (24 * 60 * 60 * 1000);
		
		return diffDays;
	}
	
	public static Date strToDate(String str, String pattern) throws Exception {
		SimpleDateFormat sf = new SimpleDateFormat(pattern);
		if (str == null) {
			return null;
		}
		return sf.parse(str);
	}

	
	 

}
